package com.hitqz.robot.biz.flow.domain.vo;

import cn.hutool.core.util.ReUtil;
import cn.hutool.json.JSONUtil;
import com.hitqz.robot.biz.flow.domain.constant.FlowConstant;
import com.yomahub.liteflow.flow.element.Node;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serial;
import java.util.List;
import java.util.Optional;

/**
 * @author wengyx
 * @date 2023/09/04
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class NodeGroupItemVo {
    @Serial
    private static final long serialVersionUID = 930998693122291091L;
    private String id;
    private String label;
    private String parentId;
    private String renderKey;
    //自定义字段
    private String name;
    private String protocol;
    private String param;
    private List<String> paramNames;
    private String description;


    public static NodeGroupItemVo convertNode2Vo(int i, Node node) {
        String protocol = Optional.ofNullable(ReUtil.get("\\[(.*?)\\]", Optional.ofNullable(node.getName()).orElse(node.getId()), 0)).orElse("无分组");
        String label = Optional.ofNullable(node.getName()).orElse(node.getId()).replace(protocol, "");
        protocol = protocol.replace("[", "").replace("]", "");
        NodeGroupItemVo dndNode = NodeGroupItemVo.builder()
                .id(String.valueOf(i))
                .label(label)
                .parentId(protocol)
                .renderKey("DND_NODE")
                .name(node.getId())
                .protocol(protocol)
                .build();
        //设置自定义信息
        setCustomInfo(dndNode);
        return dndNode;
    }

    private static void setCustomInfo(NodeGroupItemVo dndNode) {
        NodeCustomInfo nodeCustomInfo = FlowConstant.NODE_CUSTOM_INFO_MAP.get(dndNode.getName());
        if (nodeCustomInfo!=null){
            dndNode.setParam(JSONUtil.formatJsonStr(nodeCustomInfo.getParam()));
            dndNode.setParamNames(nodeCustomInfo.getParamNames());
            dndNode.setDescription(nodeCustomInfo.getDescription());
        }
    }

}
